package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Role;
import cn.wolfcode.mapper.RoleMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService{
    @Autowired
    private RoleMapper roleMapper;

    public void delete(Long id) {
        roleMapper.deleteByPrimaryKey(id);
        roleMapper.deleteRelation(id);

    }

    public void insert(Role role, Long[] ids) {
        roleMapper.insert(role);
        if(ids!=null&&ids.length>0){
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    public Role select(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    public List<Role> listAll() {
        return roleMapper.selectAll();
    }

    public void update(Role role, Long[] ids) {
          roleMapper.updateByPrimaryKey(role);

        roleMapper.deleteRelation(role.getId());
        if(ids!=null&&ids.length>0){
            roleMapper.insertBatchRelation(role.getId(),ids);
        }
    }

    @Override
    public PageInfo<Role> query(QueryObject qo) {
        PageHelper.startPage(qo.getPageNum(),qo.getPageSize());
        List<Role> result=roleMapper.selectForList(qo);

        return new PageInfo(result);
    }





}
